##################################
##################################
###Figure 4.2: Feature Barplots###
##################################
##################################
library(gganimate)
library(ggplot2)
library(maps)
library(ggthemes)
library(tidyr)
library(doBy)
library(broom)
library(dplyr)
library(lubridate)
#Set WD
setwd("~/OneDrive - Indiana University/FromGoogle/SouthSudanProject/Code_12_20_24/")
#Import data
ALLNGO <- read.csv("ingodatmap_9_20_24.csv")
summary(ALLNGO)

#############################
###Clean and Organize Data###
#############################
#Set up categories
ALLNGO$Main <- ifelse(ALLNGO$Climate.Adaptation==1,"Climate adaptation",
                      ifelse(ALLNGO$Environmental.Preservation==1, "Environmental preservation",
                             ifelse(ALLNGO$Environmental.Management==1, "Environmental management",
                                    ifelse(ALLNGO$Food.Security==1, "Food security",
                                           ifelse(ALLNGO$Conflict.prevention.early.warning==1, "Preventing conflict",
                                                  ifelse(ALLNGO$Peacebuilding == 1, "Peacebuilding",
                                                         ifelse(ALLNGO$Early.Warning == 1, "Warning","Other")))))))
table(ALLNGO$Main)

#Create an aggregated indicator for CAFSI
ALLNGO$tot <- ifelse((ALLNGO$Climate.Adaptation==1|ALLNGO$Environmental.Preservation==1|ALLNGO$Environmental.Management==1|ALLNGO$Food.Security==1),1,0) 
table(ALLNGO$tot)
#Create an aggregated indicator for CPP
ALLNGO$cp <- ifelse((ALLNGO$Conflict.prevention.early.warning==1|ALLNGO$Peacebuilding == 1|ALLNGO$Early.Warning == 1),1,0)
table(ALLNGO$cp)
#Create an exclusive for multiple intervention variables
ALLNGO$cafsi <- ifelse(ALLNGO$tot==1,"CAFSI","CPP")
table(ALLNGO$cafsi)

####################
###Program length###
####################
###Create a time indicator
ALLNGO$ym<- ymd(paste(ALLNGO$year,ALLNGO$month,1))
#Create a time trend indicator
t <- 1:length(unique(ALLNGO$ym))
g <- summaryBy(month+year~ym, data=ALLNGO, keep.names = T)
g <- g[order(g$year, g$month),]
g$ym <- NULL
t2 <- data.frame(cbind(g,t))
summary(t2)
#Aggregate into the data
ALLNGO<- left_join(ALLNGO, t2)
summary(ALLNGO$t )

##Summarize project lenth by program focus
#Define a program focus variable
ALLNGO$`Program Focus` <- as.factor(ALLNGO$Main)
#Project ID
ALLNGO$ProjectID <- paste(as.numeric(as.factor(ALLNGO$ProjectName)))

#Remove NAs
ALLNGO.s <- ALLNGO[!is.na(ALLNGO$`Program Focus`),]

#Summarize means
means.dat <- summaryBy(t~Main, data=ALLNGO.s, FUN=("mean"), keep.names = TRUE, na.rm=T)
means.dat
#Create year indicator
means.dat$`Time in years` <- round(means.dat$t/12,1)
names(means.dat) <- c("Program Focus", "Time in months", "Time in years")
means.dat


#######################
###Plot Figure 4.2a ###
#######################
# Plot Figure
jpeg("Figure42a.jpeg", width = 6, height = 6, units = 'in', res = 500)
ggplot(data=means.dat, aes(x=`Program Focus`, y=`Time in years`, fill=`Time in years`)) +
  geom_col() +
  ylab("Avearge Project time (in years)") +
  xlab("Program Focus") +
  scale_y_continuous(breaks=seq(0, 8, by=2), labels=c("0", "2", "4", "6", "8")) +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()

######################
###Project by Donor###
######################
###Collapse donor data by program focus
donor.dat <- summaryBy(StateDonor+NonstateDonors+MixedDonor~Main, data=ALLNGO.s, FUN=c("mean"), na.rm=T, keep.names = T)
donor.dat$`Program Focus` <- donor.dat$Main

# Reshape data to long format
donor_long <- donor.dat %>%
  pivot_longer(cols = c(StateDonor, NonstateDonors, MixedDonor), 
               names_to = "Donor Type", 
               values_to = "Share")

#######################
###Plot Figure 4.2b ###
#######################
# Create the stacked bar plot
jpeg("Figure42b.jpeg", width = 6, height = 6, units = 'in', res = 500)
ggplot(data = donor_long, aes(x = Main, y = Share, fill = `Donor Type`)) +
  geom_bar(stat = "identity", position = "fill") +
  ylab("Share (Proportion)") +
  xlab("Main Program Focus") +
  scale_y_continuous(labels = scales::percent_format()) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()



